package com.itheima.stock.controller.v1;

import com.alibaba.excel.EasyExcel;
import com.itheima.stock.api.StockControllerAPI;
import com.itheima.stock.domain.StockExcelDomain;
import com.itheima.stock.entity.StockBusiness;
import com.itheima.stock.mapper.StockMarketIndexInfoMapper;
import com.itheima.stock.mapper.StockMarketLogPriceMapper;
import com.itheima.stock.properties.MarketProperties;
import com.itheima.stock.response.ResponseResult;
import com.itheima.stock.service.StockService;
import com.itheima.stock.vo.*;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Author:gzc
 * @Date: 2025/10/23 16:02
 * @Description:
 */
@Slf4j
@RestController
public class StockController implements StockControllerAPI {

    private final StockService stockService;



    public StockController(StockService stockService, StockMarketIndexInfoMapper stockMarketIndexInfoMapper, MarketProperties marketProperties, StockMarketLogPriceMapper stockMarketLogPriceMapper) {
        this.stockService = stockService;
    }

    @Override
    public ResponseResult<List<StockBusiness>> getAllStockBusiness() {
        return ResponseResult.ok(stockService.getAllStockBusiness());
    }

    @Override
    public ResponseResult<List<InnerMarketVO>> innerIndexAll() {
        return ResponseResult.ok(stockService.innerIndexAll());
    }

    @Override
    public ResponseResult<List<SectorAllVO>> sectorAll() {
        return ResponseResult.ok(stockService.sectorAll());
    }

    @Override
    public ResponseResult<List<IncreaseVO>> increase() {
        return ResponseResult.ok(stockService.increase());
    }

    @Override
    public ResponseResult<StockAllVO<IncreaseVO>> stockAll(Integer page, Integer pageSize) {
        return ResponseResult.ok(stockService.stockAll(page, pageSize));
    }

    @Override
    public ResponseResult<UpDownCountVO<CountVO>> upDownCount() {
        return ResponseResult.ok(stockService.upDownCount());
    }

    @Override
    public void export(Integer page, Integer pageSize, HttpServletResponse response) {
        try {
            //1.设置响应数据的类型:excel
            response.setContentType("application/vnd.ms-excel");
            //2.设置响应数据的编码格式
            response.setCharacterEncoding("utf-8");
            //3.设置默认的文件名称
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("stockRt", StandardCharsets.UTF_8);
            //设置默认文件名称
            response.setHeader("content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //4.分页查询股票数据
            StockAllVO<IncreaseVO> stockAllVO = stockService.stockAll(page, pageSize);
            //5.导出
            EasyExcel.write(response.getOutputStream(), StockExcelDomain.class).sheet("股票数据").doWrite(stockAllVO.getRows());
        } catch (IOException e) {
            log.info("股票excel数据导出异常，当前页：{}，每页大小：{}，异常信息：{}",page,pageSize,e.getMessage());
        }
    }

    @Override
    public ResponseResult<TradeVolVO<CountVO>> tradeVol() {
        return ResponseResult.ok(stockService.tradeVol());
    }

    @Override
    public ResponseResult<UpDownVO<InfoVO>> upDown() {
        return ResponseResult.ok(stockService.upDown());
    }

    @Override
    public ResponseResult<List<TimeSharingVO>> timeSharing(String code) {
        return ResponseResult.ok(stockService.timeSharing(code));
    }

    @Override
    public ResponseResult<List<dkLineVO>> dkLine(String code) {
        return ResponseResult.ok(stockService.dkLine(code));
    }
}

